package com.leejxx.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leejxx.health.dao.MenuDao;
import com.leejxx.health.dao.PermissionDao;
import com.leejxx.health.dao.RoleDao;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.exception.BusinessRuntimeException;
import com.leejxx.health.pojo.Menu;
import com.leejxx.health.pojo.Permission;
import com.leejxx.health.pojo.Role;
import com.leejxx.health.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author LeeJx
 * @ClassName RoleServiceImpl
 * @Description TODO
 * @date 2021/1/11 19:53
 * @Version 1.0
 */
@DubboService
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = roleDao.findByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public Role findPermissionsAndMenus() {
        LinkedHashSet<Permission> permissions = permissionDao.findAllPermission();
        LinkedHashSet<Menu> menus = menuDao.findAllMenus();
        Role role = new Role();
        role.setPermissions(permissions);
        role.setMenus(menus);
        return role;
    }

    @Override
    @Transactional
    public void addRole(Role role, List<Integer> permissionIds, List<Integer> menuIds) {
        if (null == role.getName() || null == role.getKeyword()) {
            throw new BusinessRuntimeException("角色名称或关键字为空，请重新输入");
        } else {
            roleDao.addRole(role);
        }

        Integer roleId = roleDao.getId(role.getName());

        if (null != permissionIds && permissionIds.size() > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.insertPermissionIdForRole(roleId,permissionId);
            }
        }
        if (null != menuIds && menuIds.size() > 0) {
            for (Integer menuId : menuIds) {
                roleDao.insertMenuIdForRole(roleId,menuId);
            }
        }

    }

    @Override
    @Transactional
    public Role findRoleById(Integer roleId) {
        Role role = roleDao.findById(roleId);
        LinkedHashSet<Permission> permissions = permissionDao.findAllPermission();
        LinkedHashSet<Menu> menus = menuDao.findAllMenus();
        List<Integer> permissionIds = roleDao.findPermissionIdsfromRole(roleId);
        List<Integer> menuIds = roleDao.findMenuIdsfromRole(roleId);

        role.setPermissions(permissions);
        role.setMenus(menus);
        role.setPermissionIds(permissionIds);
        role.setMenuIds(menuIds);
        return role;
    }

    @Override
    @Transactional
    public void edit(Role role, List<Integer> permissionIds, List<Integer> menuIds) {
        if (null == role.getName() || null == role.getKeyword()) {
            throw new BusinessRuntimeException("角色名称或关键字为空，请重新输入");
        } else {
            roleDao.editRole(role);
        }
        Integer roleId = role.getId();
        //先删后增
        if (null != permissionIds && permissionIds.size() > 0) {
            roleDao.deletePermissionIdForRole(roleId);
            for (Integer permissionId : permissionIds) {
                roleDao.insertPermissionIdForRole(roleId,permissionId);
            }
        }else {
            roleDao.deletePermissionIdForRole(roleId);
        }

        if (null != menuIds && menuIds.size() > 0) {
            roleDao.deleteMenuIdForRole(roleId);
            for (Integer menuId : menuIds) {
                roleDao.insertMenuIdForRole(roleId, menuId);
            }
        } else {
            roleDao.deleteMenuIdForRole(roleId);
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer roleId) {
        Long countOfPermission = roleDao.findCountOfPermissionByRoleId(roleId);
        Long countOfMenu = roleDao.findCountOfMenuByRoleId(roleId);
        if (countOfPermission > 0 || countOfMenu > 0) {
            throw new BusinessRuntimeException("该角色下有绑定数据，无法删除");
        } else if (countOfPermission == 0 && countOfMenu == 0) {
            roleDao.deleteRoleByRoleId(roleId);
        }
    }
}
